home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / CHASSIS_ / MENUPROC.C < prev    next >
Text File  |  1992-05-14  |  9KB  |  288 lines

  1. /************************************************************************************/
  2. /*    MenuProc                                                                        */
  3. /*      This proc is entered either from the MouseDownProc with the inMenuBar            */
  4. /*    condition, or from the KeyDownProc when the Command key has been pressed,        */
  5. /*    which simulates a mouseDown on the menu, or from a call before the main event    */
  6. /*    loop if the finder is passing the names of files to be opened or printed upon    */
  7. /*    startup.                                                                        */
  8. /************************************************************************************/
  9.  
  10. #include "MyHeaders.h"
  11.  
  12. int MenuProc(long menuLong)
  13. {
  14.     int        MenuRetCode = 0;
  15.     long    workLong;
  16.     int        windSubSave;
  17.  
  18.  
  19.     mymenuID = HiWord(menuLong);
  20.     mymenuItem = LoWord(menuLong);
  21.     
  22.     switch (mymenuID)
  23.         {
  24.         case (AppleMenuID):                        /* If in Apple Menu...                */
  25.             switch (mymenuItem)
  26.                 {
  27.                 case (AboutItem):                /*    ...About                        */
  28.                     WorkRetCode = ShowInfoDlog (128);
  29.                 break;                            /* end of About                        */
  30.  
  31.                 case (HelpItem):                /*  ...Help                            */
  32.                     WorkRetCode = HelpGetProc();
  33.                 break;                            /* end of Help                        */
  34.                         
  35.                 default:                        /*  ...Desk Accessories                */
  36.                     GetItem(myMenuHandle[0], mymenuItem, &menuItemName);
  37.                     OpenDeskAcc(menuItemName);
  38.                 break;                            /* end of Desk Accessories            */
  39.                 }
  40.         break;                                    /* end of Apple Menu                */
  41.  
  42.         case (FileMenuID):                        /* If in File Menu...                */
  43.             switch (mymenuItem)
  44.                 {
  45.                 case (NewItem):
  46.                     WorkRetCode = TextGetProc();
  47.                 break;
  48.  
  49.                 case (OpenItem):
  50.                     WorkRetCode = FileOpenProc();            /* open the file        */
  51.                     if (WorkRetCode == 0)
  52.                         {
  53.                         WorkRetCode = GetEOF (workPathRefNum, &worklong);
  54.                         if (WorkRetCode == noErr)
  55.                             {
  56.                             if (worklong <= 32767)            /* check for max length    */
  57.                                 {
  58.                                 WorkRetCode = TextGetProc();    /* open a window    */
  59.                                 if (WorkRetCode == 0)
  60.                                     {
  61.                                     SetWTitle (windTbl[windSub].windPtr, /* title    */
  62.                                             workReply.fName);        
  63.                                     windTbl[windSub].windReply =        /* store it    */
  64.                                             workReply;
  65.                                     windTbl[windSub].windPathRefNum =    /* store it    */
  66.                                             workPathRefNum;
  67.                                     worklong =                            /* read in    */
  68.                                             ReadIntoTE(workPathRefNum,
  69.                                             windTbl[windSub].windTEH[0]);
  70.                                     }
  71.                                 else                            /* window not avail    */
  72.                                     MenuRetCode = 1;            /* set high ret cod    */
  73.                                 }
  74.                             else                            /* file too large         */
  75.                                 {
  76.                                 ParamText (&workReply.fName,"","","");    /* set name    */
  77.                                 PlaceAlert (135);            /* position the alert    */
  78.                                 StopAlert (135, NIL);        /* show the alert        */
  79.                                 MenuRetCode = 1;            /* set high ret code    */
  80.                                 }
  81.                             }
  82.                         else                                /* bad read of EOF         */
  83.                             {
  84.                             ParamText (&workReply.fName,"","","");    /* set name        */
  85.                             PlaceAlert (136);                /* position the alert    */
  86.                             StopAlert (136, NIL);            /* show the alert        */
  87.                             MenuRetCode = 1;                /* set high ret code    */
  88.                             }
  89.                         }
  90.                     else                                    /* other failed to open    */
  91.                         MenuRetCode = 1;                    /* set high ret code    */
  92.                         
  93.                 windTbl[windSub].windTEChanged = FALSE;            /* initialize        */
  94.                 break;
  95.                 
  96.                 case (SaveItem):
  97.                     WorkRetCode = FileSaveProc(FALSE);        /* save                    */
  98.                     if ((WorkRetCode != 0)                    /* if not good            */
  99.                             && (WorkRetCode != 2))            /* and not cancel        */
  100.                         {
  101.                         ParamText (&workReply.fName,"","","");    /* set name            */
  102.                         PlaceAlert (137);                    /* position the alert    */
  103.                         StopAlert (137, NIL);                /* show the alert        */
  104.                         }
  105.                     else
  106.                         WorkRetCode = MenuUpdate(ProcText);    /* update menus            */
  107.                 break;
  108.                 
  109.                 case (SaveAsItem):
  110.                     WorkRetCode = FileSaveProc(TRUE);        /* save as                */
  111.                     if ((WorkRetCode != 0)                    /* if not good            */
  112.                             && (WorkRetCode != 2))            /* and not cancel        */
  113.                         {
  114.                         ParamText (&workReply.fName,"","","");    /* set name            */
  115.                         PlaceAlert (137);                    /* position the alert    */
  116.                         StopAlert (137, NIL);                /* show the alert        */
  117.                         }
  118.                     else
  119.                         WorkRetCode = MenuUpdate(ProcText);    /* update menus            */
  120.  
  121.                 break;
  122.                 
  123.                 case (PageSetupItem):
  124.                     CursorSelect (NIL, NIL, NIL);            /* set cursor to arrow    */
  125.                     UseResFile(initResRefNum);                /* just manager resfils    */
  126.                     PrStlDialog(prRecHdl);                    /* run the style dialog    */
  127.                 break;
  128.                 
  129.                 case (PrintItem):
  130.                     workBoolean = TRUE;                        /* initialize            */
  131.                     CursorSelect(NIL, NIL, NIL);            /* set arrow cursor        */
  132.                     UseResFile(initResRefNum);                /* just manager resfils    */
  133.                     if (PrValidate(prRecHdl))                /* if different printer    */
  134.                         workBoolean = PrStlDialog(prRecHdl);    /* run style dialog    */
  135.                     if (workBoolean)                        /* if still ok            */
  136.                         workBoolean = PrJobDialog(prRecHdl);    /* run job dialog    */
  137.                     if (workBoolean)                        /* if still okay        */
  138.                         PrintWindowProc(windSub);             /*   go to print        */
  139.                     else                                    /* otherwise            */
  140.                         MenuRetCode = 1;                    /*   set bad return        */
  141.                 break;
  142.  
  143.                 case (CloseItem):
  144.                     workWindowPtr = FrontWindow();    /* which is front (active) ?    */
  145.                     WorkRetCode = 
  146.                             WhichWindow(workWindowPtr, &k);    /* match ptr to table    */
  147.                     if (WorkRetCode == 0)                    /* if it's a good match    */
  148.                         {                                    /* then                    */
  149.                         windSub = k;                        /*   set the subscrpt    */
  150.                         switch (windTbl[windSub].windRec.refCon)    /* window type    */
  151.                             {
  152.                             case (ProcText):
  153.                                 WorkRetCode = TextCloseProc();
  154.                             break;
  155.                             case (ProcHelp):
  156.                                 WorkRetCode = HelpCloseProc();
  157.                             break;
  158.                             }
  159.                         }
  160.                 break;
  161.                 
  162.                 case (QuitItem):                /* ...Quit                            */
  163.                     for (j=0; j<windMax; j++)                /* check all windows    */
  164.                         {
  165.                         if (windTbl[j].windRec.refCon == ProcText)    /* if text wind    */
  166.                             {
  167.                             windSubSave = windSub;                /* save window sub    */
  168.                             windSub = j;
  169.                             WorkRetCode = TextCloseProc();            /* close it        */
  170.                             windSub = windSubSave;                /* restore wind sub    */
  171.                             if (WorkRetCode != noErr)                /* if cancel    */
  172.                                 break;                                /* abort loop    */
  173.                             }
  174.                         else
  175.                         if (windTbl[j].windRec.refCon == ProcHelp)    /* if help wind    */
  176.                             WorkRetCode = HelpCloseProc();            /* close it        */
  177.                         }
  178.                     if (j == windMax)            /* if we got to the end    (no cancel)    */
  179.                         myQuitFlag = 1;            /* set the quit flag in event loop    */
  180.                 break;                            /* end of Quit                        */
  181.                 }
  182.         break;                                    /* end of File Menu                    */
  183.         
  184.         case (EditMenuID):                        /* If in Edit Menu                    */
  185.             workWindowPtr = FrontWindow();            /* which is front (active) ?    */
  186.             WorkRetCode = 
  187.                     WhichWindow(workWindowPtr, &k);            /* match ptr to table    */
  188.             if (WorkRetCode == 0)                            /* if it's a good match    */
  189.                 {                                            /* then                    */
  190.                 windSub = k;                                /*   set the subscrpt    */
  191.                 switch (windTbl[windSub].windRec.refCon)    /* window type            */
  192.                     {
  193.                     case (ProcText):
  194.                         workTEHandle = windTbl[windSub].windTEH[0];    /* get handles    */
  195.                         workControlHandle = windTbl[windSub].windCntlH[0];
  196.                         
  197.                         switch (mymenuItem)
  198.                             {
  199.                             case (CutItem): 
  200.                                 TECut(workTEHandle);
  201.                                 myActionProc (workControlHandle, 0);
  202.                                 windTbl[windSub].windTEChanged = TRUE;
  203.                                 worklong = noErr + 1;        /* not equal to noErr    */
  204.                                 if (ZeroScrap() == noErr)
  205.                                     worklong = TEToScrap();
  206.                                 if (worklong != noErr)
  207.                                     {
  208.                                     worklong = ZeroScrap();
  209.                                     worklong = TEFromScrap();
  210.                                     PlaceAlert (143);
  211.                                     StopAlert (143,NIL);
  212.                                     }
  213.                                 WorkRetCode = MenuUpdate(ProcText);
  214.                             break;
  215.                             
  216.                             case (CopyItem):
  217.                                 TECopy(workTEHandle);
  218.                                 myActionProc (workControlHandle, 0);
  219.                                 worklong = noErr + 1;        /* not equal to noErr    */
  220.                                 if (ZeroScrap() == noErr)
  221.                                     worklong = TEToScrap();
  222.                                 if (worklong != noErr)
  223.                                     {
  224.                                     worklong = ZeroScrap();
  225.                                     worklong = TEFromScrap();
  226.                                     PlaceAlert (143);
  227.                                     StopAlert (143,NIL);
  228.                                     }
  229.                                 WorkRetCode = MenuUpdate(ProcText);
  230.                             break;                            
  231.                             
  232.                             case (PasteItem):
  233.                                 worklong = TEFromScrap();
  234.                                 if (worklong == noErr)
  235.                                     {
  236.                                     if (    (
  237.                                                 TEGetScrapLen()
  238.                                                 + (**workTEHandle).teLength
  239.                                                 + (**workTEHandle).selStart
  240.                                                 - (**workTEHandle).selEnd
  241.                                             )
  242.                                             > 32767)
  243.                                         {
  244.                                         PlaceAlert (132);
  245.                                         StopAlert (132,NIL);
  246.                                         }
  247.                                     else
  248.                                         TEPaste(workTEHandle);
  249.                                         myActionProc (workControlHandle, 0);
  250.                                         windTbl[windSub].windTEChanged = TRUE;
  251.                                     }
  252.                                 else
  253.                                     {
  254.                                     worklong = ZeroScrap();
  255.                                     worklong = TEFromScrap();
  256.                                     PlaceAlert (143);
  257.                                     StopAlert (143,NIL);
  258.                                     }
  259.                                 WorkRetCode = MenuUpdate(ProcText);
  260.                             break;
  261.  
  262.                             case (ClearItem):
  263.                                 TEDelete (workTEHandle);                /* clear    */
  264.                                 myActionProc (workControlHandle, 0);    /* rescroll    */
  265.                                 windTbl[windSub].windTEChanged = TRUE;    /* changed    */
  266.                                 WorkRetCode = MenuUpdate(ProcText);    /* update menus    */
  267.                             break;
  268.                             }
  269.                     break;
  270.                     }
  271.                 }
  272.         break;                                    /* end of Edit Menu                    */
  273.         
  274.         case (ProgramMenuID):                    /* If in Program Menu                */
  275.             switch (mymenuItem)
  276.                 {
  277.                 case (KudosItem):
  278.                     WorkRetCode = ShowInfoDlog (130);
  279.                 break;
  280.                 }
  281.         break;                                    /* end of Program Menu                */
  282.         }
  283.         
  284.     HiliteMenu(0);                                /* turn highlight off                */
  285.  
  286.     return MenuRetCode;
  287. }
  288.